//
//  NSNumber+PriceAttr.m
//  mesu
//
//  Created by dl on 2018/7/6.
//  Copyright © 2018年 gdmesu. All rights reserved.
//

#import "NSNumber+PriceAttr.h"
#import "NSString+NWEmpty.h"
#import "UIColor+NWHex.h"

@implementation NSNumber (PriceAttr)

- (NSAttributedString *)priceAttrfont:(UIFont *)font
                          decimalFont:(UIFont *)decimalFont {
    return [self priceAttrfont:font
                   decimalFont:decimalFont
                     prefix:@"￥"];
}

- (NSAttributedString *)priceAttrfont:(UIFont *)font
                          decimalFont:(UIFont *)decimalFont
                               prefix:(NSString *)prefix {
    long price = self.integerValue / 100;
    NSMutableString *priceStr = [NSMutableString string];
    if ([NSString nw_isNotEmptyString:prefix]) {
        [priceStr appendString:prefix];
    }
    [priceStr appendFormat:@"%ld", price];
    NSUInteger priceLength = priceStr.length - 1;
    
    NSInteger point = self.integerValue % 100;
    if (point != 0) {
        if (point % 10 != 0) {
            if (point < 10) {
                [priceStr appendFormat:@".0%ld", (long) point];
            } else {
                [priceStr appendFormat:@".%ld", (long) point];
            }
        } else {
            [priceStr appendFormat:@".%ld", (long) (point / 10)];
        }
    }
    
    NSMutableAttributedString *priceAttr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [priceAttr addAttribute:NSForegroundColorAttributeName value:[UIColor nw_colorWithHex:0xF9000C] range:NSMakeRange(0, priceStr.length)];
    [priceAttr addAttribute:NSFontAttributeName value:decimalFont range:NSMakeRange(0, priceStr.length)];
    [priceAttr addAttribute:NSFontAttributeName value:font range:NSMakeRange(1, priceLength)];
    return [priceAttr copy];
}

@end
